﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class AccessoryStockOutList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
            }
        }
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = "";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and (RequestCode like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (txtReceiver.Text.Trim() != "")
            {
                Whereclause += " and outid in (select Outid from dbo.Cms_Wsm_StockInOut where Receiver in(select userid from Cms_Sys_User where UserName like '%" + txtReceiver.Text.Trim()+ "%'))";
            }

            if (txtAccessoryCompanyNum.Text.Trim() != "")
            {
                Whereclause += " and outid in (select Outid from dbo.Cms_Wsm_StockInOut where StockID in (select StockID from Cms_Wsm_AccessoryStock where AccessoryCompanyNum = '" + txtAccessoryCompanyNum.Text.Trim() + "'))";
            }

            if (txtAccessoryCode.Text.Trim() != "")
            {
                Whereclause += " and outid in (select Outid from dbo.Cms_Wsm_StockInOut where StockID in (select StockID from Cms_Wsm_AccessoryStock where AccessoryCode = '" + txtAccessoryCode.Text.Trim() + "'))";
            }

            int RecordCount = 0;
            List<WsmAccessoryOutEntity> list = new SysUserBll().GetAll<WsmAccessoryOutEntity>(Whereclause, "CreatedOn desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = list;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = "";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and (RequestCode like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (txtReceiver.Text.Trim() != "")
            {
                Whereclause += " and outid in (select Outid from dbo.Cms_Wsm_StockInOut where Receiver in(select userid from Cms_Sys_User where UserName like '%" + txtReceiver.Text.Trim() + "%'))";
            }

            if (txtAccessoryCompanyNum.Text.Trim() != "")
            {
                Whereclause += " and outid in (select Outid from dbo.Cms_Wsm_StockInOut where StockID in (select StockID from Cms_Wsm_AccessoryStock where AccessoryCompanyNum = '" + txtAccessoryCompanyNum.Text.Trim() + "'))";
            }

            if (txtAccessoryCode.Text.Trim() != "")
            {
                Whereclause += " and outid in (select Outid from dbo.Cms_Wsm_StockInOut where StockID in (select StockID from Cms_Wsm_AccessoryStock where AccessoryCode = '" + txtAccessoryCode.Text.Trim() + "'))";
            }

            DataTable dt = new StockBll().ExportAccessoryStockOutList(Whereclause, " AccessoryCode");

            Export.Export2Excel(dt, "出库明细", Page.Response);

        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "delete")
            {
                Label OutID = e.Item.FindControl("OutID") as Label;
                List<WsmAccessoryStockEntity> listCheck = new SystemBll().GetAll<WsmAccessoryStockEntity>(" and StockID in (select StockID from Cms_Wsm_StockInOut where OutID = '" + OutID.Text + "') and AccessoryStatus > 1");
                if (listCheck != null && listCheck.Count > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "alert('单据中的配件已在使用状态，不能删除！')", true);
                    return;
                }
                List<WsmAccessoryStockEntity> listupdate = new SystemBll().GetAll<WsmAccessoryStockEntity>(" and StockID in (select StockID from Cms_Wsm_StockInOut where OutID = '" + OutID.Text + "')");
                foreach (WsmAccessoryStockEntity entity in listupdate)
                {
                    entity.AccessoryStatus = 0;
                    new SystemBll().Update(entity);
                }
                new SystemBll().Delete<WsmStockInOutEntity>(" and OutID = '" + OutID.Text + "'");
                new SystemBll().Delete<WsmAccessoryOutEntity>(" and OutID = '" + OutID.Text + "'");
                BindList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //Label lblOperatorValue = e.Item.FindControl("lblOperatorValue") as Label;
            //Label lblRequestBy = e.Item.FindControl("lblRequestBy") as Label;
            //Label lblRequestByValue = e.Item.FindControl("lblRequestByValue") as Label;
            //if (lblOperator != null && lblOperatorValue != null)
            //{
            //    SysUserEntity entity = new SysUserBll().Get<SysUserEntity>(new Guid(lblOperatorValue.Text));
            //    lblOperator.Text = entity.UserName;

            //    entity = new SysUserBll().Get<SysUserEntity>(new Guid(lblRequestByValue.Text));
            //    lblRequestBy.Text = entity.UserName;
            //}
        } 
    }
}
